<template>
  <div>
    <BasicTable @register="registerTable" :loading="true" title="站点管理">
      <template #toolbar>
        <a-button type="primary" @click="onAddStationButtonClick">新增</a-button>
        <a-button type="primary" @click="onMoreDept"> 批量部门设置 </a-button>
      </template>
      <template #action="{ column, record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditStationButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:audit-outlined',
              label: '设置部门',
              color: 'warning',
              onClick: onSetDepts.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: onDeleteStationButtonClick.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddOrUpdateStation @register="registerAddOrUpdateStationModal" @success="afterSuccess" />
    <deptTreeModel @register="registerDeptTree" @success="afterSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { useGlobSetting } from '/@/hooks/setting';
  import { stationTableColumns } from './station.data';
  import { queryAllStation, deleteStation } from '/@/api/qms/station';
  import deptTreeModel from './DeptTreeModel.vue';
  import { StationDto } from '/@/api/qms/model/stationModel';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();
  //使用modal弹出窗口
  import { useModal } from '/@/components/Modal';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import AddOrUpdateStation from './AddOrUpdateStation.vue';
  export default defineComponent({
    name: 'stationIndex',
    components: { BasicTable, TableAction, AddOrUpdateStation, deptTreeModel },
    setup(_, {}) {
      const { globalProjectCode } = useGlobSetting();
      const projectCode = ref<String>(globalProjectCode);
      const [registerAddOrUpdateStationModal, { openModal: openAddOrUpdateStaion }] = useModal();
      const [registerDeptTree, { openModal: openDeptTree }] = useModal();
      const [registerTable, { reload, getSelectRowKeys }] = useTable({
        api: queryAllStation, //api中指定的为table加载数据对应的查询方法
        columns: stationTableColumns,
        canResize: true,
        striped: false,
        showTableSetting: true,
        bordered: true,
        ellipsis: true,
        indexColumnProps: {
          width: 150,
        },
        actionColumn: {
          width: 250,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
        rowSelection: { type: 'checkbox' },
        rowKey: 'stcd',
        clickToRowSelect: false, //开启点击行选中/取消checkbox
      });
      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        reload();
      }
      /**
       * 打开新增窗口,将左侧选中的部门id传过去，默认所属部门为左侧选中部门
       */

      function onAddStationButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '站点管理',
          operateType: OperationTypeEnum.ADD,
        };
        openAddOrUpdateStaion(true, propsData);
      }
      /**
       * 打开编辑窗口
       * @param record
       */
      function onEditStationButtonClick(record: StationDto) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '站点管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.stcd,
        };
        openAddOrUpdateStaion(true, propsData);
      }

      /**
       * 删除
       * @param record
       */
      function onDeleteStationButtonClick(record: StationDto) {
        deleteStation(record.stcd)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }
      /**
       * 打开设置部门窗口
       */
      function onSetDepts(record: Recordable) {
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '站点管理',
          operateString: '设置关联部门',
          dataId: record.stcd,
          projectCode: projectCode.value,
        };
        openDeptTree(true, propsData);
      }
      /**
       * 打开批量设置部门窗口
       */
      function onMoreDept() {
        if (getSelectRowKeys().length == 0) {
          createMessage.warning('请勾选站点');
        } else {
          let propsData: QcUnifyTransToChildProps = {
            moduleName: '站点管理',
            operateString: '设置关联部门',
            stcds: getSelectRowKeys(),
            projectCode: projectCode.value,
          };
          openDeptTree(true, propsData);
        }
      }
      return {
        registerTable,
        registerAddOrUpdateStationModal,
        afterSuccess,
        onSetDepts,
        onMoreDept,
        projectCode,
        registerDeptTree,
        onAddStationButtonClick,
        onEditStationButtonClick,
        onDeleteStationButtonClick,
      };
    },
  });
</script>
